/*
 *  UVCCamera
 *  library and sample to access to UVC web camera on non-rooted Android device
 *
 * Copyright (c) 2014-2017 saki t_saki@serenegiant.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  All files in the folder are under this Apache License, Version 2.0.
 *  Files in the libjpeg-turbo, libusb, libuvc, rapidjson folder
 *  may have a different license, see the respective files.
 */

package com.cdzs.twocamera.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;

import com.cdzs.twocamera.myinterface.AspectRatioViewInterface;

/**
 * 改变 View 的大小，来保持特定的长宽比
 * 如果将该 view 放在 frameLayout，设置 "android:layout_gravity="center"，会看见view显示在屏幕中间并保持特定的长宽比，显示效果更好，图像不会形变
 *
 * API >= 14
 *
 */
public class SimpleUVCCameraTextureView extends TextureView implements AspectRatioViewInterface {

    private double mRequestedAspect = -1.0;

    public SimpleUVCCameraTextureView(final Context context) {
        this(context, null, 0);
    }

    public SimpleUVCCameraTextureView(final Context context, final AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleUVCCameraTextureView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    /**
     * 显示比率为负数，直接抛出异常
     * @param aspectRatio
     */
    @Override
    public void setAspectRatio(final double aspectRatio) {
        if (aspectRatio < 0) {
            throw new IllegalArgumentException();
        }
        // 设置显示比率，需重新布局
        if (mRequestedAspect != aspectRatio) {
            mRequestedAspect = aspectRatio;
            requestLayout();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (mRequestedAspect > 0) {
            int initialWidth = MeasureSpec.getSize(widthMeasureSpec);
            int initialHeight = MeasureSpec.getSize(heightMeasureSpec);

            final int horizPadding = getPaddingLeft() + getPaddingRight();
            final int vertPadding = getPaddingTop() + getPaddingBottom();

            // 初始化宽度减去上、下、左、右的 padding 间距
            initialWidth -= horizPadding;
            initialHeight -= vertPadding;

            // 初始化宽度 / 高度 = 显示比率
            final double viewAspectRatio = (double) initialWidth / initialHeight;

            // 预设比率 与 控件比率的差值
            final double aspectDiff = mRequestedAspect / viewAspectRatio - 1;

            if (Math.abs(aspectDiff) > 0.01) {
                if (aspectDiff > 0) {
                    // 视频预设高度偏大，适当放大控件高度
                    initialHeight = (int) (initialWidth / mRequestedAspect);
                } else {
                    // // 视频预设宽度偏大，适当放大控件宽度
                    initialWidth = (int) (initialHeight * mRequestedAspect);
                }

                // 图像界面大小 + padding 大小 = 实际控件大小
                initialWidth += horizPadding;
                initialHeight += vertPadding;

                // 确定控件大小
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(initialWidth, MeasureSpec.EXACTLY);
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(initialHeight, MeasureSpec.EXACTLY);
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
